package com.hhit.collaborative_learning.community;

import java.io.IOException;
import java.sql.Connection;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.hhit.collaborative_learning.dao.BBS_ReplyDao;
import com.hhit.collaborative_learning.dao.BBS_ReplyMessageDao;
import com.hhit.collaborative_learning.dao.impl.BBS_ReplyDaoImpl;
import com.hhit.collaborative_learning.dao.impl.BBS_ReplyMessageDaoImpl;
import com.hhit.collaborative_learning.utils.DBAccess;
import com.hhit.collaborative_learning.utils.DateUtil;
import com.hhit.collaborative_learning.utils.ResponseUtil;
import com.hhit.collaborative_learning.utils.StringUtil;

import net.sf.json.JSONObject;

/**
 * Servlet implementation class setReplyManagerServlet
 */
@WebServlet("/community/setReplyManagerServlet")
public class setReplyManagerServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public setReplyManagerServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String[] idArray=request.getParameterValues("idArray[]");
		String tyle=request.getParameter("tyle");
		String tId=request.getParameter("tId");
		String ids=StringUtil.formatArraytoString(0,idArray,",");
		Connection con = DBAccess.getInstance().getConn();
		JSONObject jobj=new JSONObject();
		System.out.println("idArray"+idArray.toString()+" tyle "+tyle+" ids "+ids);
		switch (tId) {
		case "-1": // 删除
			System.out.println("tyle "+tyle +" 删除");
			jobj=deletReply(con,ids);
			break;
		default:
			System.out.println("非法类型  "+tyle);
			break;
		}
		try {
			ResponseUtil.write(response, jobj);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			DBAccess.getInstance().close(con, null, null);
			System.out.println("respons object :"+jobj.toString());
		}
	}
	private JSONObject deletReply(Connection con,String ids){
		JSONObject jobj=new JSONObject();
		BBS_ReplyDao  rd=new BBS_ReplyDaoImpl();
		BBS_ReplyMessageDao rmd=new BBS_ReplyMessageDaoImpl();
		int re=rd.deleteReply(con, " ID ", ids);
		int rem=rmd.delet(con, " ReplyID ", ids);
		if (re == 0) {
			jobj.put("isSuccess", "false");
		} else {
			jobj.put("isSuccess", "true");
		}
		jobj.put("delnum", re);
		return jobj;
	}

}
